function findTableColumn (list, groups, parent) {
  const groupName = groups.shift()
  let current = list.find(item => item.title === groupName)
  if (!current) {
    current = { title: groupName, align: 'center', children: [] }
    parent ? parent.children.push(current) : list.push(current)
  }
  if (groups.length) {
    const sub = findTableColumn(current.children, groups, current)
    if (sub) {
      return sub
    }
  }
  return current
}

export function getColumns (columnDefine) {
  const { tableHeader } = columnDefine.find((item) => {
    return (item.type = 'LBCX')
  })
  if (tableHeader) {
    const temp = JSON.parse(tableHeader)
    const columns = []
    temp.forEach(item => {
      const header = {
        title: item.tableHeaderName,
        dataIndex: item.sqlConfigName,
        width: item.width || undefined,
        align: item.align || undefined
      }
      if (item.group) {
        const cgroup = findTableColumn(columns, item.group.split('-'))
        cgroup.children.push(header)
      } else {
        columns.push(header)
      }
    })
    return columns
  }
  return null
}
